package com.cn.por.config.security;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 自定义AjaxAuthenticationEntryPoint，处理未认证用户访问
 * 未授权资源时抛出的异常
 * @author 26983
 *
 */
@Component
public class AjaxAuthenticationEntryPoint implements AuthenticationEntryPoint{

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException {
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("code", 401);
		result.put("message", "匿名用户，请先登陆再访问！");
		ObjectMapper om = new ObjectMapper();
		String resStr = om.writeValueAsString(result);
		response.getWriter().write(resStr);
		
	}

}
